package asapIs134CK.womi.client.games.logoGame.froggitTheFrog.editor;

import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitEnd;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitSand;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitStart;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitWall;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.drop.SimpleDropController;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SelectionCellPanel extends VerticalPanel{
	private CellGUI _currentSelection;
	private final PickupDragController dragControl = new PickupDragControllerWithSyso(RootPanel.get(), false);
	
	private class PickupDragControllerWithSyso extends PickupDragController{

		private class DefaultDropCtrl extends SimpleDropController{

			public DefaultDropCtrl(Widget dropTarget) {
				super(dropTarget);
			}
			
			@Override
			public void onDrop(DragContext context) {
				//System.out.println("no drop here ! hahahahaha <-- Sir Vador laught");
			}
			
		}
		
		public PickupDragControllerWithSyso(AbsolutePanel boundaryPanel,
				boolean allowDroppingOnBoundaryPanel) {
			super(boundaryPanel, allowDroppingOnBoundaryPanel);
			//System.out.println("PickupDC init done");
			this.registerDropController(new DefaultDropCtrl(RootPanel.get()));
		}
		
		@Override
		public void dragStart() {
			//System.out.println("PickupDC:dragStart called");
			super.dragStart();
		}
		@Override
		public void dragEnd() {
			//System.out.println("PickupDC:dragEnd called");
			super.dragEnd();
		}
		
		
	}
	
	
	/*private class NewSelectionClickHandler implements ClickHandler{

		private CellGUI _type;
		
		public NewSelectionClickHandler(CellGUI type){
			_type = type;
		}
		@Override
		public void onClick(ClickEvent event) {
			System.out.println("detected Selection!");
			_currentSelection = (CellGUI) _type.clone();
		}
		
	}*/
	
	public SelectionCellPanel(){
		_currentSelection = null;
		
		CellGUI wallCell = new CellGUI(new FroggitWall());
		CellGUI sandCell = new CellGUI(new FroggitSand());
		CellGUI startCell = new CellGUI(new FroggitStart()); 
		CellGUI endCell = new CellGUI(new FroggitEnd());
		
		//wallCell.addClickHandler(new NewSelectionClickHandler(wallCell));
		dragControl.makeDraggable(wallCell);
		//wallCell.addClickHandler(new TempClickHandler());
		dragControl.makeDraggable(sandCell);
		dragControl.makeDraggable(startCell);
		dragControl.makeDraggable(endCell);
		
		
		this.add(wallCell);
		this.add(sandCell);
		this.add(startCell);
		this.add(endCell);
		
		
	}
	
	public CellGUI getCurrentSelection(){
		return _currentSelection;
	}
	
	@Override
	public boolean remove(Widget w) {
		int idx = getWidgetIndex(w);
		if (idx != -1 && w instanceof CellGUI) {
			CellGUI clone = ((CellGUI) w).clone();
			dragControl.makeDraggable(clone);
			insert(clone, idx);
		}
		return super.remove(w);
	}
	
	public PickupDragController getDragCtrl(){
		return dragControl;
	}
}